using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Stormwind.Core.Configuration {
	/// <summary>
	/// The configuration section for Stormwind.
	/// </summary>
	public class StormwindConfiguration : ConfigurationSection, IConfiguration {

		/// <summary>
		/// The data broker being used currently. Full name.
		/// </summary>
		[ConfigurationProperty("dataBroker", DefaultValue = "Stormwind.Core.Data.ARDataBroker, Stormwind.Core", IsRequired = true, IsKey = true)]
		public string DataBroker {
			get {
				return Convert.ToString(this["dataBroker"]);
			}
		}

		private static IConfiguration current;
		/// <summary>
		/// Current configuration.
		/// </summary>
		public static IConfiguration Current {
			get {
				try {
					if (current == null) {
						StormwindConfiguration sc = ConfigurationManager.GetSection("stormwind") as StormwindConfiguration;
						if (sc == null) {
							throw new ConfigurationErrorsException("No configuration section by the name of stormwind found in the AppDomain's config file. Please check to ensure that your config file contains the configuration needed for Stormwind.");
						}
						current = sc;
					}
					return current;
				}
				catch (System.Exception ex) {
					throw new ConfigurationErrorsException(string.Format("An error occurred while trying to retrieve the configuration from the AppDomain's config file. Please check the inner exception. Inner Error: {0}.", ex.GetBaseException().Message), ex);
				}
			}
		}
	}
}
